package com.foreveross.action.weixin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;














import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.SYWebService.vo.DataVo;
import com.foreveross.common.CommonUtil;
import com.foreveross.common.Exception.SYHttpclientCallException;
import com.foreveross.service.LogService;
import com.foreveross.service.TenementCostService;
import com.foreveross.util.JSONUtil;
import com.foreveross.vo.TenementCost;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.vo.condition.TenementCostCondition;

@Controller
@RequestMapping(value = "/weixin/tenementCostController")
public class TenementCostController {

	private Logger log = Logger.getLogger(TenementCostController.class);

	@Resource
	private TenementCostService tenementCostService;

	@Resource
	private LogService logService;
	
	/**
	 * 通过思源接口 得到当前用户的费用列表信息
	 * @param request
	 * @param response
	 * @param cusID 客户ID
	 * @param startTime 账单开始时间
	 * @param endTime 账单结束时间
	 * @param orgId 楼盘项目ID
	 * @param feeType 费用类型   tenement 剔除车费 ,    car 车相关的费用
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/sysUserRev_GetFeeList")
	public void sysUserRev_GetFeeList(HttpServletRequest request,HttpServletResponse response, String cusID, 
			String startTime, String endTime, String orgId,String feeType,
			String currentPage,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			List<TenementCostCondition> listTCC = Arrays.asList(JSONUtil.jsonToClazz(cusID ,TenementCostCondition[].class));
			//set集合保存的是引用不同地址的对象  
	        Set<TenementCostCondition> ts = new HashSet<TenementCostCondition>();  
	        ts.addAll(listTCC);  
			
			List<DataVo> dataVoList = new ArrayList<DataVo>();
			for (TenementCostCondition tenementCostCondition : ts){
				String getCusId = tenementCostCondition.getCusId();
				String getOrgId = tenementCostCondition.getOrgId();
				List<DataVo> data = tenementCostService.synUserRev_GetFeeList(getCusId, startTime, endTime, getOrgId);
				for (DataVo dataVo : data){
					dataVoList.add(dataVo);
				}
			}
					
			//按时间降序排序
			Collections.sort(dataVoList, new Comparator<DataVo>() {
				@Override
				public int compare(DataVo o1, DataVo o2) {
					return o2.getRepYears().compareTo(o1.getRepYears());
				}
			});
			
			List<DataVo> carFeeDataVoList = new ArrayList<DataVo>();
			List<DataVo> tenementFeeDataVoList = new ArrayList<DataVo>();
			for (DataVo dataVo : dataVoList){
				if (dataVo.getIpItemName().indexOf("车") != -1){
					carFeeDataVoList.add(dataVo);
				} else {
					tenementFeeDataVoList.add(dataVo);
				}
			}
			
			PageVo<DataVo> pageVo = null;
			List<DataVo> list = null;
			if (feeType.equalsIgnoreCase("car")){
				pageVo = new PageVo<DataVo>(currentPage,carFeeDataVoList.size(),pageSize);
				list = carFeeDataVoList.subList(pageVo.getpageIndex(),
						pageVo.getpageIndex()+pageVo.getpageSize()>pageVo.gettotal()?pageVo.gettotal():pageVo.getpageIndex()+pageVo.getpageSize());
			} else if (feeType.equalsIgnoreCase("tenement")) {
				pageVo = new PageVo<DataVo>(currentPage,tenementFeeDataVoList.size(),pageSize);
				list = tenementFeeDataVoList.subList(pageVo.getpageIndex(),
						pageVo.getpageIndex()+pageVo.getpageSize()>pageVo.gettotal()?pageVo.gettotal():pageVo.getpageIndex()+pageVo.getpageSize());
			}
			
			if (list.size() > 0){
				pageVo.setData(list);
				resultVo.setData(pageVo);
				resultVo.setSuccess(true);
			} else {
				SYHttpclientCallException.throwSYHttpclientCallNULLException();
			}
		}catch(SYHttpclientCallException e){
			resultVo.setSuccess(false);
			resultVo.setMessage(e.getMessage());
		}catch (Exception e) {
			resultVo.setMessage("得到当前用户的费用列表信息失败！");
			resultVo.setSuccess(false);
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 通过思源接口 得到当前房屋的费用列表信息
	 * @param request
	 * @param response
	 * @param resID 房屋资源ID
	 * @param startTime 账单开始时间
	 * @param endTime 账单结束时间
	 * @param orgId 楼盘项目ID
	 * @param feeType 费用类型   tenement 剔除车费 ,    car 车相关的费用
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/sysUserRev_GetFeeListByRes")
	public void sysUserRev_GetFeeListByRes(HttpServletRequest request,HttpServletResponse response, String resID, 
			String startTime, String endTime, String orgId,String feeType,String userId,
			String currentPage,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			List<DataVo> dataVoList = new ArrayList<DataVo>();
			List<DataVo> data = tenementCostService.sysUserRev_GetFeeListByRes(resID, startTime, endTime, orgId, userId);
			for (DataVo dataVo : data){
				dataVoList.add(dataVo);
			}
					
			//按时间降序排序
			Collections.sort(dataVoList, new Comparator<DataVo>() {
				@Override
				public int compare(DataVo o1, DataVo o2) {
					return o2.getRepYears().compareTo(o1.getRepYears());
				}
			});
			
			List<DataVo> carFeeDataVoList = new ArrayList<DataVo>();
			List<DataVo> tenementFeeDataVoList = new ArrayList<DataVo>();
			for (DataVo dataVo : dataVoList){
				if (dataVo.getIpItemName().indexOf("车") != -1){
					carFeeDataVoList.add(dataVo);
				} else {
					tenementFeeDataVoList.add(dataVo);
				}
			}
			
			PageVo<DataVo> pageVo = null;
			List<DataVo> list = null;
			if (feeType.equalsIgnoreCase("car")){
				pageVo = new PageVo<DataVo>(currentPage,carFeeDataVoList.size(),pageSize);
				list = carFeeDataVoList.subList(pageVo.getpageIndex(),
						pageVo.getpageIndex()+pageVo.getpageSize()>pageVo.gettotal()?pageVo.gettotal():pageVo.getpageIndex()+pageVo.getpageSize());
			} else if (feeType.equalsIgnoreCase("tenement")) {
				pageVo = new PageVo<DataVo>(currentPage,tenementFeeDataVoList.size(),pageSize);
				list = tenementFeeDataVoList.subList(pageVo.getpageIndex(),
						pageVo.getpageIndex()+pageVo.getpageSize()>pageVo.gettotal()?pageVo.gettotal():pageVo.getpageIndex()+pageVo.getpageSize());
			}
			
			if (list.size() > 0){
				pageVo.setData(list);
				resultVo.setData(pageVo);
				resultVo.setSuccess(true);
			} else {
				SYHttpclientCallException.throwSYHttpclientCallNULLException();
			}
		}catch(SYHttpclientCallException e){
			resultVo.setSuccess(false);
			resultVo.setMessage(e.getMessage());
		}catch (Exception e) {
			resultVo.setMessage("得到当前用户的费用列表信息失败！");
			resultVo.setSuccess(false);
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/getTenementCostList")
	public void getTenementCostList(HttpServletRequest request,
			HttpServletResponse response, String currentPage,String pageSize,
			String wechatId,String userId, String houseId,
			String IpItemName, String status,String resCode,String type) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) tenementCostService.getTenementCostListCount(wechatId,userId,houseId,
					 IpItemName,status,resCode,type);
			PageVo<TenementCost> pageObj = new PageVo<TenementCost>(currentPage, countRecord,pageSize);
			pageObj.setData(tenementCostService.findTenementCostList(wechatId,userId,houseId,
					 IpItemName,status,resCode,type, pageObj.getpageIndex(),pageObj.getpageSize()));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("缴费记录 查看失败");
			log.error("缴费记录 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 查看缴费详情
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/getTenementCost")
	public void getTenementCost(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			TenementCost hrHouses = (TenementCost) tenementCostService.findObject(
					TenementCost.class, id);
			resultVo.setSuccess(true);
			resultVo.setData(hrHouses);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看缴费账单信息失败");
		}
		List list = Arrays.asList("houseUser");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo, list);
	}
}
